'use strict'

var _typeof = typeof Symbol == 'function' && typeof Symbol.iterator == 'symbol' ? function(obj) { return typeof obj } : function(obj) { return obj && typeof Symbol == 'function' && obj.constructor === Symbol && obj !== Symbol.prototype ? 'symbol' : typeof obj }

function _newArrowCheck(innerThis, boundThis) { if (innerThis !== boundThis) { throw new TypeError('Cannot instantiate an arrow function') } }

(function(global, factory) {
    (typeof exports == 'undefined' ? 'undefined' : _typeof(exports)) == 'object' && typeof module != 'undefined' ? factory(exports) : typeof define == 'function' && define.amd ? define(['exports'], factory) : factory(global.$solway = global.$solway || {})
}(window, function(exports) {
    _newArrowCheck(void 0, void 0)

    var zoom = function(_ref) {
        var _ref$scale = _ref.scale,
            _scale = _ref$scale === void 0 ? 1 : _ref$scale,
            _ref$minScale = _ref.minScale,
            minScale = _ref$minScale === void 0 ? 0.1 : _ref$minScale,
            _ref$maxScale = _ref.maxScale,
            maxScale = _ref$maxScale === void 0 ? 14 : _ref$maxScale,
            ele = _ref.ele,
            _ref$rate = _ref.rate,
            rate = _ref$rate === void 0 ? 0.1 : _ref$rate,
            _ref$translate = _ref.translate,
            translate = _ref$translate === void 0 ? [0, 0] : _ref$translate,
            _mousex = '', _mousey = '',
            coordinate = {}

        _newArrowCheck(void 0, void 0)

        var obj = $(ele).css({
            transition: 'all 0.1s',
            '-moz-transition': 'all 0.1s',
            '-webkit-transition': 'all 0.1s',
            '-o-transition': 'all 0.1s',
            position: 'absolute',
            cursor: 'pointer',
            left: 0,
            top: 0
        }).on('mousewheel DOMMouseScroll', mouseWheelHandel)
        function getDistance(bg, end){
          return Math.sqrt(Math.pow((end.x - bg.x),2 ) + Math.pow((end.y - bg.y),2 ))
        }
        $(ele)[0].addEventListener('touchstart', e => {
          
          let x = e.touches[0].pageX
          let y = e.touches[0].pageY
          if (e.touches.length > 1) {
            coordinate.start = getDistance({
              x: e.touches[0].pageX, 
              y: e.touches[0].pageY,
            },{
                x: e.touches[1].pageX, 
                y: e.touches[1].pageY,
            })
          }
        })
        
        $(ele)[0].addEventListener('touchmove', function(e){
          if(e.touches.length === 2){
            // 计算移动过程中的两个手指的距离
            coordinate.stop = getDistance({
                x: e.touches[0].pageX,
                y: e.touches[0].pageY,
            },{
                x: e.touches[1].pageX,
                y: e.touches[1].pageY,
            })
            // 设置缩放尺寸
            let scaleSize = (coordinate.stop / coordinate.start)  - 1
            scaleFunc(scaleSize/5)
          }
        }, false)
        $(ele)[0].addEventListener('touchend', function(e){
        }, false)
        function onmousemove(event) {
            var event = event || window.event
            if (event.offsetX || event.offsetY) {
            _mousex = event.offsetX
            _mousey = event.offsetY
            // console.log('鼠标x', _mousex)
            // console.log('鼠标y', _mousey)
            } else if (event.layerX || event.layerY) {
                // console.log((event.layerX-1) + "" + (event.layerY-1))
            // return (event.layerX-1) + "" + (event.layerY-1);
            }
        }
        function onmouseleave(event) {
            var event = event || window.event
            _mousex = ''
            _mousey = ''
        }
        $(ele).mousemove(onmousemove)
        $(ele).mouseleave(onmouseleave)
        scaleFunc(0, !0)

        function mouseWheelHandel(e) {
            var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) || (e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1))
            if (delta > 0) return scaleFunc(rate)
            else if (delta < 0) return scaleFunc(-rate)
        }

        function scaleFunc(val, origin) {
            if (window.SVGDATA.scale === 100) _scale = 1
            _scale += (val * _scale)
            _scale = _scale <= minScale ? minScale : _scale
            _scale = _scale >= maxScale ? maxScale : _scale
            window.SVGDATA.scale = Math.ceil(_scale * 100)
            if (_scale < minScale || _scale > maxScale) return !1
            if (_scale === 1) {
                document.getElementsByClassName('draggable')[0].style.top = '0'
                document.getElementsByClassName('draggable')[0].style.left = '0'
                document.getElementById('dragDiv').style.left = '0'
                document.getElementById('dragDiv').style.top = '0'
            }
            obj.css({
                transform: 'matrix(' + _scale + ',0,0,' + _scale + ', ' + translate[0] + ',' + translate[1] + ')',
                '-ms-transform': 'matrix(' + _scale + ',0,0,' + _scale + ', ' + translate[0] + ',' + translate[1] + ')',
                '-moz-transform': 'matrix(' + _scale + ',0,0,' + _scale + ', ' + translate[0] + ',' + translate[1] + ')',
                '-webkit-transform': 'matrix(' + _scale + ',0,0,' + _scale + ', ' + translate[0] + ',' + translate[1] + ')',
                '-o-transform': 'matrix(' + _scale + ',0,0,' + _scale + ', ' + translate[0] + ',' + translate[1] + ')',
                'transform-origin': `${_mousex}px ${_mousey}px 0px`,
                '-ms-transform-origin': `${_mousex}px ${_mousey}px 0px`,
                '-webkit-transform-origin': `${_mousex}px ${_mousey}px 0px`,
                '-o-transform-origin': `${_mousex}px ${_mousey}px 0px`,
                '-moz-transform-origin': `${_mousex}px ${_mousey}px 0px`
            })
            // drag里放svg 在chrome中  svg的text标签会在缩放移动后消失 这句是是为了触发更新 把text显示出来  
            // edge中正常 不知道为毛
            document.getElementById('zoomSvg').style.top = ''
            return !1
        }

        return {
            scale: (function() {
                function scale(n) {
                    scaleFunc(n)
                }
                return scale
            }()),

            destroy: (function() {
                function destroy() {
                    obj.off('mousewheel DOMMouseScroll', mouseWheelHandel)
                }

                return destroy
            }())
        }
    }.bind(void 0)

    exports.zoom = zoom
}.bind(void 0)))